<template>
  <div class="login">
    <div class="login-box">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <h3>登录</h3>
        <el-form-item label="账号" prop="user">
          <el-input type="text" v-model="ruleForm.user" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
        </el-form-item>
        <p>账号:admin 密码:12345</p>
        <el-button
          type="primary"
          :loading="isLoading"
          class="loginIn"
          @click="submitForm('ruleForm')"
        >登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        // if (this.ruleForm.user !== "") {
        //   this.$refs.ruleForm.validateField("user");
        // }
        console.log(34);
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        // if (this.ruleForm.pwd !== "") {
        //   this.$refs.ruleForm.validateField("pwd");
        // }
        callback();
      }
    };
    return {
      ruleForm: {
        user: "",
        pwd: ""
      },
      rules: {
        user: [{ validator: validatePass1, trigger: "blur" }],
        pwd: [{ validator: validatePass2, trigger: "blur" }]
      },
      isLoading:false
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$store.dispatch('userLogin',{user:this.ruleForm.user,pwd:this.ruleForm.pwd}).then(()=>{
            this.isLoading = true
            this.$router.push({
              path:this.$route.query.redirect || '/'
            })
          })
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang='scss' scoped>
.login {
  width: 100%;
  height: 100vh;
  background-image: url("../assets/images/banner2.png");
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  .login-box {
    width: 500px;
    height: 500px;
    position: absolute;
    right: 150px;
    top: 50%;
    margin-top: -250px;
    background: rgba($color: #fff, $alpha: 0.3);
    border-radius: 10px;
    .el-form {
      height: 100%;
      > h3 {
        text-align: center;
        line-height: 100px;
        font-size: 25px;
        color: #fff;
      }
      /deep/ .el-form-item__label {
        color: #fff;
      }
      /deep/ .el-input {
        width: 80%;
      }
      p{
        color: #000;
        text-align: center;
      }
      .loginIn {
        display: block;
        margin: 0 auto;
      }
    }
  }
}
</style>